package com.caiyi.financial.nirvana.sms.gateway.sgip;

import com.caiyi.financial.nirvana.sms.gateway.DefaultGateWay;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class SGIPServerManager {

    private static final Logger logger = LoggerFactory.getLogger(SGIPServerManager.class);

    private static SGIPServerManager instance;

    private Map<Integer, SGIPServer> servers;

    private SGIPServerManager() {
        servers = new HashMap<>();
    }

    public static synchronized SGIPServerManager getInstance() {
        if (instance == null) {
            instance = new SGIPServerManager();
        }
        return instance;
    }

    /*
     * 设置SGIP网关对应的SGIPserver，如果没有则创建server并且启动监听
     */
    public synchronized void push(DefaultGateWay gateWay) {
        if (gateWay instanceof SGIPClient) {
            int port = 55005;
            SGIPServer server = servers.get(port);
            if (server == null) {
                logger.info("create SGIP server,on port:{}", port);
                server = new SGIPServer(port, (SGIPClient) gateWay);
                new Thread(server).start();
                servers.put(port, server);
            }
        }
    }

}
